﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiBoost.Core.Plugins.Gui.Admin.ContentTypeManager.Model;
using EPiBoost.Plugins.ContentTypeManager;
using EPiBoost.Plugins.ContentTypeManager.Model;
using EPiServer.PlugIn;

namespace EPiBoost.Core.Plugins.Gui.Admin.ContentTypeManager
{
	//Resource is registered by EPiBoost.Core.Plugins.Gui.Admin.ContentTypeManager.RegisterResources

	[GuiPlugIn(DisplayName = "EPiBoost - Content Type Manager", Description = "Remove unmapped properties",
		Area = PlugInArea.AdminMenu, Url = RegisterResources.FilePath)]
	public partial class ContentTypeManager : System.Web.UI.Page
	{
		private List<ContentTypeInfo> ContentTypes;
		private bool HideMappedProperties = true;



		protected void Page_Init(object sender, EventArgs e)
		{
			rptContentTypes.ItemDataBound += rptContentTypes_ItemDataBound;
			btnRemove.Click += btnRemove_Click;
			
			BindData(); 
		}

		private void BindData()
		{
			ContentTypes = ContentTypeLoader.LoadPageTypes();
			if (HideMappedProperties)
			{
				ContentTypes =
					ContentTypes.Where(x => x.ContentProperties.Any(y => !y.ExistsOnModel)).OrderBy(x => x.IsPageType).ToList();
			}
			rptContentTypes.DataSource = ContentTypes;
			rptContentTypes.DataBind();
		}

		void btnRemove_Click(object sender, EventArgs e)
		{
			var selectedProperties = GetSelectedProperties();
			ContentTypeLoader.RemoveProperties(selectedProperties);
			BindData(); 
		}

	 

		private List<ContentTypeInfo> GetSelectedProperties()
		{
			var result = new List<ContentTypeInfo>();
			foreach (Control repeaterItem in rptContentTypes.Controls)
			{
				 
				var contentType = new ContentTypeInfo();
				var rptContentProperties = repeaterItem.FindControl("rptContentProperties") as Repeater;
				var hdnContentTypeGuid = repeaterItem.FindControl("hdnContentTypeGuid") as HiddenField;
			 
				contentType.ContentTypeGuid = new Guid((hdnContentTypeGuid).Value);
				contentType.ContentTypeName = ContentTypes.Where(x => x.ContentTypeGuid == contentType.ContentTypeGuid).FirstOrDefault().ContentTypeName;

				foreach (Control repeaterItemProp in rptContentProperties.Controls)
				{
					var hdnPropertyId = repeaterItemProp.FindControl("hdnPropertyId") as HiddenField;
					var chkRemove = repeaterItemProp.FindControl("chkRemove") as CheckBox;

					if (chkRemove.Checked)
					{
						contentType.ContentProperties.Add(new ContentPropertyInfo()
							{
								PropertyDefinitionId = int.Parse(hdnPropertyId.Value)
							});
					}
				}

				if (contentType.ContentProperties.Any())
				{
					result.Add(contentType);
				}
			}
			return result;
		}

		void rptContentTypes_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var item = e.Item.DataItem as ContentTypeInfo;
			var rptContentProperties = e.Item.FindControl("rptContentProperties") as Repeater;
			var hdnContentTypeGuid = e.Item.FindControl("hdnContentTypeGuid") as HiddenField;
			
			var properties = item.ContentProperties;
			if (HideMappedProperties)
			{
				properties = properties.Where(x => !x.ExistsOnModel).ToList();
			}

			hdnContentTypeGuid.Value = item.ContentTypeGuid.ToString();

			rptContentProperties.ItemDataBound += rptContentProperties_ItemDataBound;
			rptContentProperties.DataSource = properties;
			rptContentProperties.DataBind();
		}

		void rptContentProperties_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			var item = e.Item.DataItem as ContentPropertyInfo;
			var hdnPropertyId = e.Item.FindControl("hdnPropertyId") as HiddenField;
			hdnPropertyId.Value = item.PropertyDefinitionId.ToString();
		}
	}
}